package com.cnfangmao.masterdata.service.impl;

import java.util.Date;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cnfangmao.masterdata.common.utils.MD5Utils;
import com.cnfangmao.masterdata.dao.AccountDao;
import com.cnfangmao.masterdata.dto.AccountDto;
import com.cnfangmao.masterdata.model.Account;
import com.cnfangmao.masterdata.service.AccountService;



/**
 * <p>
 * 主数据中心用户 服务实现类
 * </p>
 *
 * @author chenxd
 * @since 2020-04-14
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountDao, Account> implements AccountService {

	private final Logger log = LoggerFactory.getLogger(AccountServiceImpl.class);
	
	@Autowired
	private AccountDao dao;
	@Override
	public Account findByUsername(String username) {
		Account account=null;
		   try{
			   account= dao.findByUsername(username);
		   }catch(Exception ex)
		   {
			   log.info("AccountServiceImpl findByUsername :",ex);
		   }
		   return account;
		
	}

	@Override
	public boolean update(AccountDto dto,String modifiedBy) {
		boolean result=false;
		try {
			Account account = dao.selectById(dto.getUserId());
			account.setUserName(dto.getUserName());
			account.setPassword(MD5Utils.getMD516(dto.getPassword()));
			account.setModifiedOn(new Date());
			account.setModifiedBy(modifiedBy);
			result=dao.updateById(account)>0;	
		} catch (Exception ex) {
			log.error("AccountServiceImpl update err : ",ex);
		}

		return result;
	}

	@Override
	public boolean delete(String userId) {
		boolean result=false;
		try {
			result=dao.deleteById(userId)>0;	
		} catch (Exception ex) {
			log.error("AccountServiceImpl delete err : ",ex);
		}

		return result;
	}

	@Override
	public boolean saveAccount(AccountDto dto,String createdBy) {
		boolean result=false;
		try {
			Account account =new Account();
			account.setUserName(dto.getUserName());
			account.setPassword(MD5Utils.getMD516(dto.getPassword()));
			account.setUserId(UUID.randomUUID().toString());
			account.setUserCode(MD5Utils.getMD516(String.valueOf(System.currentTimeMillis()+dto.getPassword())).toUpperCase());
			account.setCreatedOn(new Date());
			account.setCreatedBy(createdBy);
			result=dao.insert(account)>0;	
		} catch (Exception ex) {
			log.error("AccountServiceImpl saveAccount err : ",ex);
		}

		return result;
	}

	@Override
	public IPage<Account> queryListAccount(String username, int pageNo, int pageSize) {

		IPage<Account> page =
	               dao.selectPage(new Page<>(pageNo,pageSize),
	               new QueryWrapper<Account>().like(StringUtils.isNotEmpty(username),"user_name",username));

         return page;
	}

}
